#include "stdafx.h"
#include "jlogindialog.h"
#include "jdbmodule.h"
#include "jappconfig.h"
#include "strhelper.h"
#include "wxhelper.h"
#include <wx/msgdlg.h>
#include "jglobals.h"
#include <wx/app.h>

JLoginDialog::JLoginDialog(wxWindow* parent)
    :JLoginDialogUI(parent)
{
    //this->m_choiceKS->SetClientObject(new TClientData<std::wstring>(L""));
}

JLoginDialog::~JLoginDialog()
{

}

void JLoginDialog::OnClose(wxCloseEvent &event)
{
    event.Skip(true);
}

void JLoginDialog::OnInitDialog(wxInitDialogEvent &event)
{
    event.Skip(true);
    for(auto & ptr : g_infoList)
    {
        this->m_choiceXL->Append(ptr->name);
    }
    if(this->m_choiceXL->GetCount()>0)
    {
        this->m_choiceXL->Select(0);
    }
    this->m_tcLoginID->SetFocus();
}

void JLoginDialog::OnTextChangedLoginID(wxCommandEvent &event)
{
    event.Skip(true);
    this->m_choiceKS->Clear();
    this->m_stTips->SetLabel(L"");
}

void JLoginDialog::OnButtonClickExit(wxCommandEvent &event)
{
    this->EndModal(wxID_EXIT);
}

void JLoginDialog::OnButtonClickLogin(wxCommandEvent &event)
{
    wxString user_id;
    wxString user_pwd;

    if(this->m_choiceXL->IsEmpty())
    {
        this->m_choiceXL->SetFocus();
        this->m_stTips->SetLabel(utf2wxs(u8"无网络线路信息！"));
        return;
    }
    user_id=this->m_tcLoginID->GetValue().Trim(true).Trim(false);
    if(user_id.IsEmpty())
    {
        this->m_tcLoginID->SetFocus();
        this->m_stTips->SetLabel(utf2wxs(u8"请输入用户ID！"));
        return ;
    }
    if(m_choiceKS->GetCount()<=0)
    {
        this->update_ks(this->m_tcLoginID->GetValue().Trim(true).Trim(false));
        m_choiceKS->SetFocus();
        this->m_stTips->SetLabel(utf2wxs(u8"请选择登录科室！"));
        return ;
    }

    user_pwd=m_tcPassword->GetValue();
    if(user_pwd.IsEmpty())
    {
        m_tcPassword->SetFocus();
        this->m_stTips->SetLabel(utf2wxs(u8"请输入密码！"));
        return ;
    }
    if(this->checkLogin())
        this->EndModal(wxID_OK);
}

void JLoginDialog::OnChoiceXL(wxCommandEvent &event)
{
    event.Skip(true);
    int nsel=event.GetSelection() ;
    if(nsel>=0)
    {
        wxString str=this->m_choiceXL->GetString(nsel);

        JAppConfig::setCurNetInfo(str.ToStdWstring());
    }
}

void JLoginDialog::OnKillFocusLoginID(wxFocusEvent &event)
{
    event.Skip(true);
    wxApp::GetInstance()->CallAfter([this](){

        wxString str=this->m_tcLoginID->GetValue().Trim(true).Trim(false);

        if(!str.IsEmpty() && this->m_choiceKS->IsEmpty())
        {
            this->update_ks(this->m_tcLoginID->GetValue().Trim(true).Trim(false));
        }
    });
}

void JLoginDialog::OnSetFocusKS(wxFocusEvent &event)
{
    event.Skip(true);
    wxApp::GetInstance()->CallAfter([this](){

        wxString str=this->m_tcLoginID->GetValue().Trim(true).Trim(false);

        if(!str.IsEmpty() && this->m_choiceKS->IsEmpty())
        {
            this->update_ks(this->m_tcLoginID->GetValue().Trim(true).Trim(false));
        }
    });

}

void JLoginDialog::OnTextChangePassWD(wxCommandEvent &event)
{
   this->m_stTips->SetLabel(L"");
}

void JLoginDialog::update_ks(const wxString &rybh)
{
    this->m_choiceKS->Clear();
    ado::ConnectionPtr con=ado::newHisCon();
    if(!con)
    {
        return ;
    }

    static const wchar_t sql_t[]=
LR"(
SELECT [KSMC],[KSDM] FROM [HIS_330400].[dbo].[ZD_RYGL_KS]
WHERE (RYBH=?) AND (KSFL='住院')
ORDER BY KSDM
)";
    //std::wstring sql=str_format(sql_t,str.wc_str());
    try {
        ado::CommandPtr cmd=ado::newCommand(con);
        cmd->PutCommandText(ws2bstr(sql_t));
        cmd->PutCommandType(ADODB::adCmdText);
        cmd->PutPrepared(VARIANT_TRUE);
        ado::ParameterPtr param=cmd->CreateParameter(EmptyString,
                                                    ADODB::adVarChar,
                                                    ADODB::adParamInput,50);
        param->PutValue(rybh.wc_str());
        cmd->GetParameters()->Append(param);
        param.Release();

        ado::RecordsetPtr rs=cmd->Execute(NULL,NULL,ADODB::adOptionUnspecified);
        while(!rs->GetadoEOF())
        {
            _bstr_t ksmc(rs->GetCollect(L"KSMC"));
            _bstr_t ksdm(rs->GetCollect(L"KSDM"));
            int nv=this->m_choiceKS->Append((const wchar_t*)ksmc);
            this->m_choiceKS->SetClientObject(nv,new TClientData<std::wstring>((const wchar_t*)ksdm));
            rs->MoveNext();
        }
    } catch (_com_error & e) {
        ado::ShowError(e);
    }

    if(this->m_choiceKS->GetCount()>0)
    {
        this->m_choiceKS->Select(0);
    }
}

bool JLoginDialog::checkLogin()
{
    if(this->m_choiceXL->IsEmpty() || this->m_choiceKS->IsEmpty())
        return false;
    wxString userID=this->m_tcLoginID->GetValue().Trim(true).Trim(false);
    wxString passWD=this->m_tcPassword->GetValue();

    if( userID.IsEmpty() || passWD.IsEmpty())
        return false;
    ado::ConnectionPtr con=ado::newHisCon();
    if(!con)
    {
        return false;
    }

    static const wchar_t sql_t[]=
LR"(
SELECT [RYXM], [RYBH],[RYXB], [RYLX]  FROM [HIS_330400].[dbo].[ZD_RYGL]
WHERE RYBH='%s' AND RYMM='%s'
)";
    wxString sql=wxString::Format(sql_t,userID,passWD);
    try {

        ado::RecordsetPtr rs=con->Execute(sql.wc_str(),NULL,-1);
        while(!rs->GetadoEOF())
        {
            LoginInfoPtr li=LoginInfoPtr(new LoginInfo);
            li->RYXM=(const wchar_t*)_bstr_t(rs->GetCollect(L"RYXM"));
            li->RYBH=(const wchar_t*)_bstr_t(rs->GetCollect(L"RYBH"));
            li->RYXB=(const wchar_t*)_bstr_t(rs->GetCollect(L"RYXB"));
            li->RYLX=(const wchar_t*)_bstr_t(rs->GetCollect(L"RYLX"));

            int nsel=this->m_choiceKS->GetSelection();
            if(nsel>=0)
            {
                li->KSMC=this->m_choiceKS->GetString(nsel).ToStdWstring();
                li->KSDM= TClientData<std::wstring>::FromClientData(this->m_choiceKS->GetClientObject(nsel))->GetData();
            }

            JGlobals::SetValue(GV_LOGIN_INFO,li);
            return true;
        }
        ado::ShowError(L"错误的用户名或密码。");

    } catch (_com_error & e) {
        ado::ShowError(e);
        return false;
    }
    return false;
}


























